package com.dxy.elasticsearch.controller;

import com.dxy.elasticsearch.entity.JobDetail;
import com.dxy.elasticsearch.service.JobDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Description 职位详情接口
 * @Author dangxianyue
 * @Date 2022/12/30 15:00
 */
@Slf4j
@Controller
@Api(tags = "职位详情接口")
@RequestMapping("/jobDetail")
public class JobDetailController {

    @Autowired
    private JobDetailService jobDetailService;

    @ApiOperation("添加一个职位数据")
    @PostMapping("/add")
    public ResponseEntity<String> add(@Valid @RequestBody JobDetail jobDetail) throws IOException {
        jobDetailService.add(jobDetail);
        return ResponseEntity.ok("success");
    }

    @ApiOperation("根据ID检索指定职位数据")
    @GetMapping("/findById/{id}")
    public ResponseEntity<JobDetail> findById(@PathVariable("id") Long id) throws IOException {
        return ResponseEntity.ok(jobDetailService.findById(id));
    }

    @ApiOperation("修改职位信息")
    @PostMapping("/update")
    public ResponseEntity<String> update(@Valid @RequestBody JobDetail jobDetail) throws IOException {
        jobDetailService.update(jobDetail);
        return ResponseEntity.ok("success");
    }

    @ApiOperation("根据ID删除职位数据")
    @GetMapping("/deleteById/{id}")
    public ResponseEntity<String> deleteById(@PathVariable("id") Long id) throws IOException {
        jobDetailService.deleteById(id);
        return ResponseEntity.ok("success");
    }

    @ApiOperation("根据关键字检索职位数据")
    @GetMapping("/searchByKeywords")
    public ResponseEntity<List<JobDetail>> searchByKeywords(String keywords) throws IOException {
        return ResponseEntity.ok(jobDetailService.searchByKeywords(keywords));
    }

    @ApiOperation("分页检索")
    @GetMapping("/searchByPage")
    public ResponseEntity<Map<String, Object>> searchByPage(String keywords, int pageNum, int pageSize) throws IOException {
        return ResponseEntity.ok(jobDetailService.searchByPage(keywords, pageNum, pageSize));
    }

    @ApiOperation("scroll分页解决深分页问题")
    @GetMapping("/searchByScrollPage")
    public ResponseEntity<Map<String, Object>> searchByScrollPage(String keywords, String scrollId, int pageSize) throws IOException {
        return ResponseEntity.ok(jobDetailService.searchByScrollPage(keywords, scrollId, pageSize));
    }
}
